//
//  NRWBulletBaseView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/7.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBulletBaseView.h"

@interface NRWBulletBaseView()<UIGestureRecognizerDelegate>

@end

@implementation NRWBulletBaseView
{
    UIView *_mSuperView;
    UIView *_mContentView;
    BOOL _mIsAnimating;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    }
    return self;
}

- (void)prepare {
    
    [self addContentView];
    
    [self layoutContentView];
    
    [self addTapGesture];
}

#pragma mark - publicMethod
- (void)setContentViewHeight:(CGFloat)contentHeight {
    
    [_mContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@(contentHeight));
    }];
}

- (void)showForView:(UIView *)view {
    
    if (self->_mIsAnimating) {
        return;
    }
    
    self.isShow = YES;
    
    [view addSubview:self];
    [view bringSubviewToFront:self];
    
    if ([self customerAnimation]) {
        
        [self showAnimationWithContentView:_mContentView backgroundView:self];
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            self->_mIsAnimating = YES;
            self.alpha = 1;
        } completion:^(BOOL finished) {
            self->_mIsAnimating = NO;
        }];
    }
}

- (void)dismiss {
    
    if (self->_mIsAnimating) {
        return;
    }
    
    self.isShow = NO;
    
    if ([self customerAnimation]) {
        
        [self dismissAnimationWithContentView:_mContentView backgroundView:self];
    } else {
        
        [UIView animateWithDuration:0.25 animations:^{
            self->_mIsAnimating = YES;
            self.alpha = 0;
        } completion:^(BOOL finished) {
            self->_mIsAnimating = NO;
            [self removeFromSuperview];
        }];
    }
}

#pragma mark - subClass override
- (UIView *)bulletContentView {
    return [UIView new];
}

- (BulletContentViewAlignment)contentViewAlignment {
    return BulletAlignmentTop;
}

- (CGFloat)alignmentMargin {
    return 0;
}

- (CGFloat)contentViewHeight {
    return 0;
}

- (BOOL)touchBackgroundCanClose {
    return YES;
}

- (BOOL)customerAnimation {
    return NO;
}

- (void)showAnimationWithContentView:(UIView *)contentView backgroundView:(NRWBulletBaseView *)bgView {
    
}

- (void)dismissAnimationWithContentView:(UIView *)contentView backgroundView:(NRWBulletBaseView *)bgView {
    
}

#pragma mark - privateMethod
- (void)addContentView {
    
    _mContentView = [self bulletContentView];
    if (_mContentView != nil) {
        [self addSubview:_mContentView];
    }
}

- (void)layoutContentView {
    
    if ([self contentViewAlignment] == BulletAlignmentTop) {
        
        [_mContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset([self alignmentMargin]);
            make.left.right.equalTo(self);
            make.height.equalTo(@([self contentViewHeight]));
        }];
    } else if ([self contentViewAlignment] == BulletAlignmentBottom) {
        
        [_mContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset([self alignmentMargin]);
            make.left.right.equalTo(self);
            make.height.equalTo(@([self contentViewHeight]));
        }];
    }
}

- (void)addTapGesture {
    
    if ([self touchBackgroundCanClose]) {
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
        tapGesture.delegate = self;
        [self addGestureRecognizer:tapGesture];
        MJWeakSelf
        [[tapGesture rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
           
            [weakSelf dismiss];
        }];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    if ([touch.view isKindOfClass:NRWBulletBaseView.class]) {
        return YES;
    }
    return NO;
}


@end
